package com.jugheadzhou.influxdb2test.controller;


import com.influxdb.client.BucketsApi;
import com.influxdb.client.DeleteApi;
import com.influxdb.client.InfluxDBClient;
import com.jugheadzhou.influxdb2test.annotations.ApiCallCounter;
import com.jugheadzhou.influxdb2test.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("influx2/test")
public class TestController {
    @Resource
    protected InfluxDBClient influxDBClient;

    @Resource
    private TestService testService;

    @ApiCallCounter
    @GetMapping("api1")
    public String api1() {
        log.info("do something for api1.");
        return "api1";
    }

    @ApiCallCounter
    @GetMapping("api2")
    public String api2() {
        log.info("do something for api2.");
        return "api2";
    }

    @ApiCallCounter
    @GetMapping("icu/temperature")
    public String icuTemperature(@RequestParam("patientId") String patientId, @RequestParam("value") String value) {
        double v = RandomUtils.nextDouble(36D, 40D);
        value = String.valueOf(v);
        log.info("collect icu temperature for patientId = {} value = {}", patientId, value);
        testService.icuTemperature(patientId, value);
        return "操作成功";
    }

    @ApiCallCounter
    @GetMapping("alert")
    public String alert() {
        log.info("do something for alert.");
        return "alert";
    }

    @PostMapping("alert")
    public String alert(@RequestBody Object params) {
        log.warn("alert_params = {}", params);
        return "alert";
    }

    @GetMapping("delete/measurement")
    public String deleteMeasurement(String name) {
        log.warn("deleteMeasurement = {}", name);
        DeleteApi deleteApi = influxDBClient.getDeleteApi();
        BucketsApi bucketsApi = influxDBClient.getBucketsApi();
        return "alert";
    }
}
